<?
/**
 * 广告位置分类
 *
 */
class Adpos extends MyBase{
	private $categorys = array();
	
	function __construct() {
		parent::__construct('adpos.id', array('keyword' => 'title', 'mark' => 'mark'));
	}
	
	/**
	 * 修改新闻
	 */
	public function edit($post){
		$id = empty($post['id']) ? 0 : intval($post['id']);
		$select = array('mark' => $post['mark'], 'isdel' => 0);
		if($id > 0){
			$select['kept'] = " AND `id`!={$id}";
		}
		$rs = $this->db->get_row($this->tbname, $select, 'id');
		if(!empty($rs)){
			return $this->rwarn(lang('already') . lang('exist'));
		}
		
		$data = array(
			'title'		=> $post['title'],
			'mark'		=> $post['mark'],
			'width'		=> $post['width'],
			'height'	=> $post['height'],
			'rank'		=> $post['rank'],
			'memo'		=> $post['memo'],
		);
		if($id > 0){
			$data['updatetime'] = TIME;
			$id = $this->db->update($this->tbname, array('id' => $id), $data);
		}else{
			$data['addtime'] = TIME;
			$id = $this->db->insert($this->tbname, $data);
		}
		if($id > 0){
			$this->cache_adpos();
		}
		return $id;
	}
	
	/**
	 * 更新排序
	 */
	public function rank($post){
		if(!is_array($post['rank']))return false;
		$iRtn = 0;
		foreach($post['rank'] as $id => $rank){
			$data = array(
				'rank'		 => $rank,
				'updatetime' => TIME,
			);
			if($this->db->update($this->tbname, array('id' => $id), $data)){
				$iRtn ++;
			}
		}
		if($iRtn > 0){
			return $this->cache_adpos();
		}
		return $iRtn;
	}

	// 删除广告位置分类
	public function del($post){
		if(!is_array($post['list']))$post['list'] = array($post['list']);
		$iRtn = 0;
		foreach($post['list'] as $id){
			if($this->db->update($this->tbname, array('id' => $id), array('isdel' => 1, 'updatetime' => TIME))){
				$iRtn ++;
			}
		}
		if($iRtn > 0){
			return $this->cache_adpos();
		}
		return $iRtn;
	}
	
	
		
	/********************************************************************************
	 * 缓存
	 ********************************************************************************/
	
	public function cache_adpos(){
		$all = $this->get_all('*', null, array('rank' => 'DESC', 'id' => 'DESC'));
		if(!empty($all['list'])){
			$id_name_list = array();
			$mark_name_list = array();
			$list_id = array();
			$list_mark = array();
			foreach ($all['list'] as $key => $row) {
				$id_name_list[$row['id']] = $row['title'];
				$mark_name_list[$row['mark']] = $row['title'];
				$list_id[$row['id']] = $row;
				$list_mark[$row['mark']] = $row;
			}
		}
		return cache_write('adposs.php', array('ids' => $id_name_list, 'marks' => $mark_name_list, 'list_id' => $list_id, 'list_mark' => $list_mark));
	}
}
?>